package AdminBD;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import adminHibernate.HibernateConnection;
import entidadesHibernate.Clase;
import entidadesHibernate.ClaseVigencia;
import entidadesHibernate.Vigencia;

public class DAOClaseVigenciaMySql implements DAOClaseVigencia {

	@SuppressWarnings("unchecked")
	public List<Vigencia> obtenerVigencias() {
		Session session = HibernateConnection.conectar();	
		
		String hql = "from Vigencia";
		
		List <Vigencia> registros = new ArrayList<Vigencia>();
		
		try { 
			
			session.beginTransaction();			
			Query query = session.createQuery(hql);
			registros = query.list();													
			session.getTransaction().commit();		
			
			}
		catch(HibernateException e) {
		       System.out.println(e);
		     } 
		finally { 
			session.close();
			}
		return registros;
	}

	@SuppressWarnings("unchecked")
	public List<Clase> obtenerClases() {
		Session session = HibernateConnection.conectar();	
		
		String hql = "from Clase";
		
		List <Clase> registros = new ArrayList<Clase>();
		
		try { 
			
			session.beginTransaction();			
			Query query = session.createQuery(hql);
			registros = query.list();													
			session.getTransaction().commit();		
			
			}
		catch(HibernateException e) {
		       System.out.println(e);
		     } 
		finally { 
			session.close();
			}
		return registros;
	}

	public float obtenerCosto(Vigencia vigencia, Clase clase) {
		
		Session session = HibernateConnection.conectar();	
		
		String hql = "from ClaseVigencia where id.idClase = "+
		clase.getId()+" and id.idVigencia = "+vigencia.getId();
		
		ClaseVigencia aux = new ClaseVigencia();
		
		try { 
			
			session.beginTransaction();			
			Query query = session.createQuery(hql);
			aux = (ClaseVigencia) query.uniqueResult();													
			session.getTransaction().commit();		
			
			}
		catch(HibernateException e) {
		       System.out.println(e);
		     } 
		finally { 
			session.close();
			}
		return aux.getCosto();
	}

}
